
var websock = null;
var global_callback = null;
var serverPort = '5209/websocket/20';	//webSocket连接端口,
var global_loading = null;
var isLoading = false;
var stock_time_out = 20000;//webSocket连接超时时间,
var timerOut = null;
var errMessage = '{"message":"连接超时，请稍后重试","suc":"false"}';
var errMessageUnconet = '{"message":"请确认通讯服务器是否正常启动","suc":"false"}';
var isConnetWebsock = false;

function getWebIP(){
    var curIP = '127.0.0.1';
    return curIP;
}

function initWebSocket(){ //初始化weosocket
    //ws地址
    var wsuri = "ws://" +getWebIP()+ ":" + serverPort;
    websock = new WebSocket(wsuri);
    websock.onmessage = function(e){
        websocketonmessage(e);
    }
    websock.onclose = function(e){
        websocketclose(e);
    }
    websock.onopen = function () {
        websocketOpen();
    }

    //连接发生错误的回调方法
    websock.onerror = function () {
        isConnetWebsock = false;
        console.log("WebSocket连接发生错误");
    }
}

// 实际调用的方法 vue_this：vue对象this， agentData：发送的数据，callback：消息的回调
function sendSock(vue_this,agentData,callback){
    global_callback = callback;
    if (websock.readyState === websock.OPEN) {
        //若是ws开启状态
        websocketsend(vue_this,agentData)
    }else {
        if(isConnetWebsock){
            callback(errMessage);
        }else{
            callback(errMessageUnconet);
        }

        initWebSocket();
    }
}

// 实际调用的方法
// function sendSock(vue_this,msgtype,agentData,callback){
//     global_callback = callback;
//     if (websock.readyState === websock.OPEN) {
//         //若是ws开启状态
//         websocketsend(vue_this,agentData)
//     }else if (websock.readyState === websock.CONNECTING) {
//         // 若是 正在开启状态，则等待1s后重新调用
//         setTimeout(function () {
//             sendSock(vue_this,msgtype,agentData,callback);
//         }, 1000);
//     }else {
//         // 若未开启 ，则等待1s后重新调用
//         setTimeout(function () {
//             sendSock(vue_this,msgtype,agentData,callback);
//         }, 1000);
//     }
// }

//数据接收
function websocketonmessage(e){
    console.log('服务器返回数据：'+e.data);
    global_loading.close();
    isLoading = false;
    clearTime();
    global_callback(e.data);
}

//数据发送
function websocketsend(v_this,agentData){
    clearTime();

    global_loading = v_this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    isLoading = true;
    websock.send(agentData);

    timerOut =setTimeout(()=>{
        if(isLoading){
            global_loading.close();
            isLoading = false;
            clearTime();
            if(isConnetWebsock){
                global_callback(errMessage);
            }else{
                global_callback(errMessageUnconet);
            }

        }
    },stock_time_out)



}

//关闭
function websocketclose(e){
    isConnetWebsock = false;
    console.log("connection closed (" + e.code + ")");
}

function websocketOpen(e){
    console.log("连接成功");
    isConnetWebsock = true;
}

//清除定时器
function clearTime() {
    clearTimeout(timerOut)
    timerOut = null;}


if ('WebSocket' in window) {
    initWebSocket();
} else {
    alert('当前浏览器 Not support websocket')
}



export{sendSock}
